﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class BomDetailDA : IBomDetailDA
    {
        private string COMPONENT_SKU_ID = "@COMPONENT_SKU_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string OWNER_ID = "@OWNER_ID";
        private string QTY = "@QTY";
        private string SEQ_NO = "@SEQ_NO";
        private string SKU_ID = "@SKU_ID";
        private string SQL_CHECK_BOM_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@BOM_DETAIL WHERE OWNER_ID = @OWNER_ID AND SKU_ID = @SKU_ID AND COMPONENT_SKU_ID = @COMPONENT_SKU_ID ";
        private string SQL_DELETE_BOM_DETAIL = " DELETE FROM @_@BOM_DETAIL WHERE  ";
        private string SQL_INSERT_BOM_DETAIL = " INSERT INTO @_@BOM_DETAIL ( WH_ID, OWNER_ID, SKU_ID, COMPONENT_SKU_ID, SEQ_NO, QTY, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @OWNER_ID, @SKU_ID, @COMPONENT_SKU_ID, @SEQ_NO, @QTY, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_BOM_DETAIL = " SELECT  ROWID, WH_ID, OWNER_ID, SKU_ID, COMPONENT_SKU_ID, SEQ_NO, QTY, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@BOM_DETAIL ";
        private string SQL_UPDATE_BOM_DETAIL = " UPDATE @_@BOM_DETAIL SET WH_ID = @WH_ID, SEQ_NO = @SEQ_NO, QTY = @QTY, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE OWNER_ID = @OWNER_ID AND SKU_ID = @SKU_ID AND COMPONENT_SKU_ID = @COMPONENT_SKU_ID  ";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckBomDetailIDUnique(string ownerID, string skuID, string componentSkuID, string whLoginID)
        {
            string sqlStr = this.SQL_CHECK_BOM_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@SKU_ID", skuID), new SqlParameter("@COMPONENT_SKU_ID", componentSkuID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, sqlStr, paras).ToString() == "1");
        }

        public bool CheckBomDetailIDUnique(DataBase dataBase, DbTransaction tran, string ownerID, string skuID, string componentSkuID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@SKU_ID", skuID), new SqlParameter("COMPONENT_SKU_ID", componentSkuID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_BOM_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteBomDetail(List<string> ownerID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string sqlTemp = this.SQL_DELETE_BOM_DETAIL + " OWNER_ID = @OWNER_ID";
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < ownerID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlTemp.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteBomDetail(List<string> ownerID, List<string> skuID, List<string> componentSkuID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string sqlTemp = this.SQL_DELETE_BOM_DETAIL + " OWNER_ID = @OWNER_ID AND SKU_ID = @SKU_ID AND COMPONENT_SKU_ID = @COMPONENT_SKU_ID";
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < ownerID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID[j]), new SqlParameter("@SKU_ID", skuID[j]), new SqlParameter("@COMPONENT_SKU_ID", componentSkuID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlTemp.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteBomDetail(DataBase dataBase, DbTransaction tran, List<string> ownerID, List<string> whLoginID)
        {
            int result = 0;
            string sqlTemp = this.SQL_DELETE_BOM_DETAIL + " OWNER_ID = @OWNER_ID";
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < ownerID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlTemp.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteBomDetail(DataBase dataBase, DbTransaction tran, List<string> ownerID, List<string> skuID, List<string> componentSkuID, List<string> whLoginID)
        {
            int result = 0;
            string sqlTemp = this.SQL_DELETE_BOM_DETAIL + " OWNER_ID = @OWNER_ID AND SKU_ID = @SKU_ID AND COMPONENT_SKU_ID = @COMPONENT_SKU_ID";
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < ownerID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID[j]), new SqlParameter("@SKU_ID", skuID[j]), new SqlParameter("@COMPONENT_SKU_ID", componentSkuID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlTemp.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllBomDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BOM_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllBomDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BOM_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public BomDetailInfo GetBomDetailByID(string ownerID, string skuID, string componentSkuID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BOM_DETAIL.Replace("@_@", whLoginID) + " WHERE OWNER_ID = @OWNER_ID AND SKU_ID = @SKU_ID AND COMPONENT_SKU_ID = @COMPONENT_SKU_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@SKU_ID", skuID), new SqlParameter("@COMPONENT_SKU_ID", componentSkuID) };
            BomDetailInfo bomDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    bomDetailInfo = new BomDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["COMPONENT_SKU_ID"].ToString(), reader["SEQ_NO"].ToString(), (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return bomDetailInfo;
        }

        public BomDetailInfo GetBomDetailByID(DataBase dataBase, DbTransaction tran, string ownerID, string skuID, string componentSkuID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BOM_DETAIL.Replace("@_@", whLoginID) + " WHERE OWNER_ID = @OWNER_ID AND SKU_ID = @SKU_ID AND COMPONENT_SKU_ID = @COMPONENT_SKU_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@SKU_ID", skuID), new SqlParameter("@COMPONENT_SKU_ID", componentSkuID) };
            BomDetailInfo bomDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                bomDetailInfo = new BomDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["COMPONENT_SKU_ID"].ToString(), reader["SEQ_NO"].ToString(), (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return bomDetailInfo;
        }

        public DataSet GetBomDetailByQueryList(List<string> sqlWhere, BomDetailQueryEntity bomdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_BOM_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!bomdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, bomdetailQuery.CurrentPage, bomdetailQuery.PageSize, bomdetailQuery.SortField, bomdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetBomDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, BomDetailQueryEntity bomdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_BOM_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!bomdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, bomdetailQuery.CurrentPage, bomdetailQuery.PageSize, bomdetailQuery.SortField, bomdetailQuery.SortDirection);
            }
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<BomDetailInfo> GetBomDetailListByID(string ownerID, string SkuID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BOM_DETAIL.Replace("@_@", whLoginID) + " WHERE OWNER_ID = @OWNER_ID AND SKU_ID = @SKU_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@SKU_ID", SkuID) };
            List<BomDetailInfo> bomDetailInfo = new List<BomDetailInfo>();
            BomDetailInfo BomDetail = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    BomDetail = new BomDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["COMPONENT_SKU_ID"].ToString(), reader["SEQ_NO"].ToString(), (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    bomDetailInfo.Add(BomDetail);
                }
            }
            return bomDetailInfo;
        }

        public List<BomDetailInfo> GetBomDetailListByID(DataBase dataBase, DbTransaction tran, string ownerID, string SkuID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_BOM_DETAIL.Replace("@_@", whLoginID) + " WHERE OWNER_ID = @OWNER_ID AND SKU_ID = @SKU_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@SKU_ID", SkuID) };
            List<BomDetailInfo> bomDetailInfo = new List<BomDetailInfo>();
            BomDetailInfo BomDetail = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                BomDetail = new BomDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["COMPONENT_SKU_ID"].ToString(), reader["SEQ_NO"].ToString(), (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                bomDetailInfo.Add(BomDetail);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return bomDetailInfo;
        }

        public int InsertBomDetail(BomDetailInfo bomDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    bomDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BomDetail_Parameters(bomDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_BOM_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertBomDetail(DataBase dataBase, DbTransaction tran, BomDetailInfo bomDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    bomDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BomDetail_Parameters(bomDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_BOM_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_BomDetail_Parameters(BomDetailInfo bomDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SKU_ID, SqlDbType.VarChar, 50), new SqlParameter(this.COMPONENT_SKU_ID, SqlDbType.VarChar, 50), new SqlParameter(this.SEQ_NO, SqlDbType.VarChar, 10), new SqlParameter(this.QTY, SqlDbType.Decimal, 13), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(bomDetailInfo.WhID))
            {
                paramArray[0].Value = bomDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = bomDetailInfo.OwnerID;
            paramArray[2].Value = bomDetailInfo.SkuID;
            paramArray[3].Value = bomDetailInfo.ComponentSkuID;
            paramArray[4].Value = bomDetailInfo.SeqNo;
            paramArray[5].Value = bomDetailInfo.Qty;
            paramArray[6].Value = bomDetailInfo.CreatedBy;
            paramArray[7].Value = bomDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateBomDetail(BomDetailInfo bomDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    bomDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BomDetail_Parameters(bomDetailInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BOM_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateBomDetail(DataBase dataBase, DbTransaction tran, BomDetailInfo bomDetailInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    bomDetailInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_BomDetail_Parameters(bomDetailInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_BOM_DETAIL.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

